class_name WeaponComponent
extends BaseComponent

#region 属性
var default_weapon_id:String
var weapon_max_amount:int = 1
var cur_weapon_index:int = -1
var weapons:Array[Weapon] = []
var slot_component:SlotComponent
var controller_component:ControllerComponent
#endregion
#region 字段
var cur_weapon:Weapon = null:
	get:
		return cur_weapon
	set(value):
		var last_weapon = cur_weapon
		cur_weapon = value
		cur_weapon_changed.emit(last_weapon,cur_weapon)
#endregion
#region 信号
signal cur_weapon_changed(last_weapon:Weapon,cur_weapon:Weapon)
signal weapons_changed(weapons:Array[Weapon])
#endregion

#region 基类方法
func init_component(args:Dictionary = {}):
	super(args)
	slot_component = entity.get_component(SlotComponent)
	controller_component = entity.get_component(ControllerComponent)
	entity.entity_data.data_changed.connect(on_weapon_data_changed)
	cur_weapon_changed.connect(_on_cur_weapon_changed)
	weapons_changed.connect(_on_weapons_changed)
	weapon_max_amount = entity.entity_data.get_attribute_value("WeaponMaxAmount")
	default_weapon_id = entity.entity_data.default_weapon_id
	controller_component.key_action_state_changed.connect(_on_key_action_state_changed)
	if weapons.size() == 0:
		obtain_weapon_by_id(default_weapon_id)
#endregion
#region 公共方法
func get_cur_weapon() -> Weapon:
	return cur_weapon
func get_weapon(idx:int) -> Weapon:
	if idx < weapons.size():
		return weapons[idx]
	return null
func obtain_weapon(weapon:Weapon):
	if weapon and weapon_max_amount > 0:
		if cur_weapon:
			if weapons.size() == weapon_max_amount:
				cur_weapon.discard()
			else:
				cur_weapon.unequip()
		var hand = slot_component.get_slot_manager().get_slot_by_type(GameEnum.SlotType.Weapon)
		weapon.obtain(entity)
		if weapon.get_parent():
			weapon.reparent(hand)
		else:
			hand.add_child(weapon)
		weapons.append(weapon)
		weapons_changed.emit(weapons)
		# 自动切换到当前武器
		switch_weapon(weapons.size() - 1)
func obtain_weapon_by_id(weapon_id:String):
	var weapon:Weapon = GameWeaponSystem.get_weapon(weapon_id) as Weapon
	if weapon:
		obtain_weapon(weapon)
func discard_weapon(idx = null):
	if not idx:
		idx = cur_weapon_index
	if idx < weapons.size():
		var weapon:Weapon = weapons[idx]
		#if weapons.size() == 1 and cur_weapon.weapon_data.weapon_id == default_weapon_id and weapon_max_amount > 0:
			#return
		weapon.discard()
		if idx == cur_weapon_index:
			if weapons.size() == 1 or (cur_weapon_index + 1) % weapons.size() >= weapon_max_amount:
				cur_weapon_index = -1
				cur_weapon = null
			else:
				cur_weapon_index = (cur_weapon_index + 1) % weapons.size() 
				cur_weapon = weapons[cur_weapon_index]
				cur_weapon.equip()
		weapons.erase(weapon)
		weapons_changed.emit(weapons)
func switch_weapon(index:int = -1):
	if index == cur_weapon_index or weapon_max_amount <= 1:
		return
	index = (cur_weapon_index + 1) % weapons.size() if index == -1 else index
	if cur_weapon:
		cur_weapon.unequip()
	cur_weapon_index = index
	cur_weapon = weapons[cur_weapon_index]
	cur_weapon.equip()
func on_weapon_data_changed(tag_space:String,tag:String,old_value,new_value):
	## TODO 监听weapon_entity_data变化
	if tag == "weapon_max_amount":
		weapon_max_amount = entity.entity_data.get_attribute_value("WeaponMaxAmount")
		for i in range(weapons.size()-1,-1,-1):
			if i >= weapon_max_amount:
				discard_weapon(i)
	elif tag == "default_weapon_id":
		default_weapon_id = entity.entity_data.default_weapon_id
#endregion
#region 私有方法
func _on_cur_weapon_changed(last_weapon:Weapon,cur_weapon:Weapon):
	pass
func _on_weapons_changed(weapons:Array):
	if weapons.size() == 0:
		obtain_weapon_by_id(default_weapon_id)
func _on_key_action_state_changed(key_action:GameEnum.KeyAction,key_action_state:GameEnum.KeyActionState):
	#if key_action == GameEnum.KeyAction.Discard and key_action_state == GameEnum.KeyActionState.Pressed:
		#discard_weapon()
	#if key_action == GameEnum.KeyAction.Interact and key_action_state == GameEnum.KeyActionState.Pressed:
		#if entity.interact_items.size() > 0:
			#entity.interact_items[0].start_interact(entity)
	#if key_action == GameEnum.KeyAction.Interact and key_action_state == GameEnum.KeyActionState.Release:
		#if entity.interact_items.size() > 0:
			#entity.interact_items[0].end_interact(entity)
	pass
#endregion
#region 生命周期函数
#endregion
